<template>
    <el-dialog class="useredit" title="编辑用户信息" :visible.sync="showEditForm" :before-close="handleClose" width="30%">
        <el-form :model="form" ref="ruleForm" :rules="rules" class="customForm">
            <el-form-item label="用户名" :label-width="formLabelWidth" prop="name" class="formItem">
                <el-input v-model="form.name" auto-complete="off" suffix-icon="icon-third-71"></el-input>
            </el-form-item>
            <el-form-item label="重置密码" :label-width="formLabelWidth" prop="password" class="formItem">
                <el-input type="password" v-model="form.password" auto-complete="off" suffix-icon="icon-third-mima-copy"></el-input>
            </el-form-item>
            <el-form-item label="登录权限" :label-width="formLabelWidth" prop="authority">
                <el-select v-model="form.authority">
                    <el-option label="管理员" value="9"></el-option>
                    <el-option label="普通用户" value="0"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="handleReset">重 置</el-button>
            <el-button type="primary" @click="handleSubmit">提 交</el-button>
        </div>
    </el-dialog>
</template>
<script>
    import util from '../util/util';
    export default {
        props:{
            showEditForm:{
                type:Boolean,
                default:false
            },
            userObj:{
                type:Object,
                required:true
            }
        },
        data(){
            return{
                form: {
                    name: '',
                    password:'',
                    authority: ''
                },
                formLabelWidth: '80px',
                rules: {
                    name:[
                        { min: 1, max: 10, message: '用户名字符长度为1~10', trigger: 'blur'},
                        { required: true, message: '用户名不能为空', trigger: 'blur'}
                    ],
                    password: [
                        { min: 6, max: 20, message: '密码长度为6~20个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        computed:{
            getUserName:function(){
                return this.userObj.userName;
            },
            getAuthority:function(){
                return this.userObj.authority;
            }
        },
        watch:{
            showEditForm:{
                handler(){
                    this.form.name = this.userObj.userName;  
                    this.form.authority = this.userObj.authority;  
                },
                deep: true
                
            }
        },
        methods:{
            handleClose(){
                this.$emit('update:showEditForm',false);
                this.$store.dispatch("updateUserData", false);
                this.handleReset();
            },
            handleSubmit(){
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        let params = { 
                            guid: this.userObj.userId,
                            username: this.form.name, 
                            password: this.form.password,
                            authority: this.form.authority
                        };
                        this.$api.user.userUpdateInfo(params).then(response=>{
                            if(response.data.code=="6000"){
                                this.$message({
                                    type: 'success',
                                    message: '修改成功!'
                                 });
                                 this.$store.dispatch("updateUserData", true);
                            }else{
                                this.$message({
                                    type: 'error',
                                    message: util.getErrorInfo(response.data.code)
                                });
                            }
                            
                        }).catch(error=>{
                        });
                        this.$emit('update:showEditForm',false);
                        this.handleReset();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
                
            },
            handleReset(){
                this.$refs.ruleForm.resetFields();
            }
        }
    }
</script>
<style lang="scss" scoped>
    .customForm{
        padding: 0 40px 0 20px;
        .formText{
            margin-bottom: 20px;
        }
        .formItem{
            margin-bottom: 20px;
        }
    }
</style>
<style lang="css" scoped>
.useredit >>> .el-dialog{
  background: url("../../static/dialogbg.png") no-repeat ;
  background-size: 100% 100%;
  box-shadow: none;
}
.useredit >>> .el-dialog__headerbtn{
  right: 12%;
  }
.useredit >>> .el-dialog__close{
  color: #fff;
  font-size: 25px;
}
.useredit >>> .el-dialog__header{
    text-align: center;
}
</style>
